9 'require shadowsocks-libev as ss';
11 var conf = 'shadowsocks-libev';
12 var cfgtypes = ['ss_local', 'ss_redir', 'ss_server', 'ss_tunnel'];
14 var callServiceList = rpc.declare({
22 render: function(stats) {
25 m = new form.Map(conf,
27 _('Instances of shadowsocks-libev components, e.g. ss-local, \
28 ss-redir, ss-tunnel, ss-server, etc. To enable an instance it \
29 is required to enable both the instance itself and the remote \
30 server it refers to.'));
32 s = m.section(form.GridSection);
34 s.cfgsections = function() {
35 return this.map.data.sections(this.map.config)
36 .filter(function(s) { return cfgtypes.indexOf(s['.type']) !== -1; })
37 .map(function(s) { return s['.name']; });
39 s.sectiontitle = function(section_id) {
40 var s = uci.get(conf, section_id);
41 return (s ? s['.type'] + '.' : '') + section_id;
43 s.renderSectionAdd = function(extra_class) {
44 var el = form.GridSection.prototype.renderSectionAdd.apply(this, arguments),
45 optionEl = [E('option', { value: '_dummy' }, [_('-- instance type --')])];
46 cfgtypes.forEach(function(t) {
47 optionEl.push(E('option', { value: t }, [t.replace('_', '-')]));
49 var selectEl = E('select', {
50 class: 'cbi-input-select',
51 change: function(ev) {
52 ev.target.parentElement.nextElementSibling.nextElementSibling
53 .toggleAttribute('disabled', ev.target.value === '_dummy');
56 el.lastElementChild.setAttribute('disabled', '');
57 el.prepend(E('div', {}, selectEl));
60 s.handleAdd = function(ev, name) {
61 var selectEl = ev.target.parentElement.firstElementChild.firstElementChild,
62 type = selectEl.value;
63 this.sectiontype = type;
64 var promise = form.GridSection.prototype.handleAdd.apply(this, arguments);
65 this.sectiontype = undefined;
68 s.addModalOptions = function(s, section_id, ev) {
69 var sdata = uci.get(conf, section_id),
70 stype = sdata ? sdata['.type'] : null;
72 s.sectiontype = stype;
74 L.resolveDefault(fs.stat('/usr/bin/' + stype.replace('_', '-')), null),
76 ]).then(L.bind(function(res) {
77 s.tab('general', _('General Settings'));
78 s.tab('advanced', _('Advanced Settings'));
79 s.taboption('general', form.Flag, 'disabled', _('Disable'));
81 ss.option_install_package(s, 'general');
83 ss.options_common(s, 'advanced');
85 if (stype === 'ss_server') {
86 ss.options_server(s, { tab: 'general' });
87 o = s.taboption('advanced', form.Value, 'local_address',
89 _('The address ss-server will initiate connections from'));
90 o.datatype = 'ipaddr';
91 ss.values_ipaddr(o, res[1]);
92 o = s.taboption('advanced', form.Value, 'local_ipv4_address',
93 _('Local IPv4 address'),
94 _('The IPv4 address ss-server will initiate IPv4 connections from'));
95 o.datatype = 'ip4addr';
96 ss.values_ip4addr(o, res[1]);
97 o = s.taboption('advanced', form.Value, 'local_ipv6_address',
98 _('Local IPv6 address'),
99 _('The IPv6 address ss-server will initiate IPv6 connections from'));
100 o.datatype = 'ip6addr';
101 ss.values_ip6addr(o, res[1]);
103 ss.options_client(s, 'general', res[1]);
104 if (stype === 'ss_tunnel') {
105 o = s.taboption('general', form.Value, 'tunnel_address',
107 _('The address ss-tunnel will forward traffic to'));
108 o.datatype = 'hostport';
115 o = s.option(form.DummyValue, 'overview', _('Overview'));
119 o.renderWidget = function(section_id, option_index, cfgvalue) {
120 var sdata = uci.get(conf, section_id);
122 return form.DummyValue.prototype.renderWidget.call(this, section_id, option_index, ss.cfgvalue_overview(sdata));
127 o = s.option(form.DummyValue, 'running', _('Running'));
132 o = s.option(form.Button, 'disabled', _('Enable/Disable'));
135 o.inputtitle = function(section_id) {
136 var s = uci.get(conf, section_id);
137 if (ss.ucival_to_bool(s['disabled'])) {
138 this.inputstyle = 'reset';
139 return _('Disabled');
141 this.inputstyle = 'save';
144 o.onclick = function(ev) {
145 var inputEl = ev.target.parentElement.nextElementSibling;
146 inputEl.value = ss.ucival_to_bool(inputEl.value) ? '0' : '1';
147 return this.map.save();
150 return m.render().finally(function() {
151 poll.add(function() {
152 return L.resolveDefault(callServiceList(conf), {})
153 .then(function(res) {
154 var instances = null;
156 instances = res[conf]['instances'];
158 if (!instances) return;
160 .filter(function(s) { return cfgtypes.indexOf(s['.type']) !== -1; })
161 .forEach(function(s) {
162 var el = document.getElementById('cbi-shadowsocks-libev-' + s['.name'] + '-running');
164 var name = s['.type'] + '.' + s['.name'],
165 running = instances.hasOwnProperty(name)? instances[name].running : false;
166 el.innerText = running ? 'yes' : 'no';